package com.zlkj.pro.modular.admin.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.admin.model.dto.MaterialAdminDTO;
import com.zlkj.pro.modular.admin.model.vo.MaterialAdminVO;
import com.zlkj.pro.modular.admin.service.MaterialAdminService;
import com.zlkj.pro.modular.common.entity.MaterialEntity;
import com.zlkj.pro.modular.common.mapper.MaterialMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author 孙铭泽
 * @date 2022/9/24
 * @time 14:28
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class MaterialAdminServiceImpl extends ServiceImpl<MaterialMapper, MaterialEntity> implements MaterialAdminService {

    @Override
    public PageResult<MaterialAdminVO> findPage(MaterialAdminDTO materialAdminDTO) {
        Integer pageNo = materialAdminDTO.getPageNo();
        Integer pageSize = materialAdminDTO.getPageSize();
        Long groupId = materialAdminDTO.getGroupId();
        LambdaQueryWrapper<MaterialEntity> materialEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        materialEntityLambdaQueryWrapper.eq(Objects.nonNull(groupId), MaterialEntity::getGroupId, groupId);
        materialEntityLambdaQueryWrapper.orderByDesc(MaterialEntity::getCreateTime);
        Page<MaterialEntity> materialEntityPage = super.page(new Page<>(pageNo, pageSize), materialEntityLambdaQueryWrapper);
        long total = materialEntityPage.getTotal();
        List<MaterialEntity> records = materialEntityPage.getRecords();
        List<MaterialAdminVO> materialAdminList = ConvertUtil.turn(records, MaterialAdminVO.class);
        return PageResultFactory.createPageResult(materialAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<MaterialAdminVO> findList(MaterialAdminDTO materialAdminDTO) {
        LambdaQueryWrapper<MaterialEntity> materialEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        List<MaterialEntity> materialEntityList = super.list(materialEntityLambdaQueryWrapper);
        return ConvertUtil.turn(materialEntityList, MaterialAdminVO.class);
    }

    @Override
    public MaterialAdminVO findDetail(MaterialAdminDTO materialAdminDTO) {
        Long id = materialAdminDTO.getId();
        LambdaQueryWrapper<MaterialEntity> materialEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        materialEntityLambdaQueryWrapper.eq(MaterialEntity::getId, id);
        MaterialEntity materialEntity = super.getOne(materialEntityLambdaQueryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(materialEntity, MaterialAdminVO.class);
    }

    @Override
    public boolean add(MaterialAdminDTO materialAdminDTO) {
        MaterialEntity materialEntity = ConvertUtil.turn(materialAdminDTO, MaterialEntity.class);
        return super.save(materialEntity);
    }

    @Override
    public boolean edit(MaterialAdminDTO materialAdminDTO) {
        MaterialEntity materialEntity = ConvertUtil.turn(materialAdminDTO, MaterialEntity.class);
        return super.updateById(materialEntity);
    }

    @Override
    public boolean delete(MaterialAdminDTO materialAdminDTO) {
        return super.removeById(materialAdminDTO);
    }

}
